package com.py.framework.schedule;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.py.framework.common.Constants;
import com.py.framework.core.common.Pagination;
import com.py.framework.core.utils.DateUtils;
import com.py.framework.entity.MsgDetailEntity;
import com.py.framework.entity.MsgRecordEntity;
import com.py.framework.entity.UserEntity;
import com.py.framework.enums.RevieveType;
import com.py.framework.service.SysMsgService;
import com.py.framework.service.UserService;

/**
 * 消息记录入库任务
 * 
 * @author Leegern
 * @date 2018年4月27日
 */
@Service
public class MsgRecordScheduler implements InitializingBean {

	/** 任务线程 **/
	private ExecutorService ses;

	/** 注入业务类服务 **/
	@Autowired
	private SysMsgService sysMsgService;
	@Autowired
	private UserService userService;

	/**
	 * 提交消息入库任务
	 * 
	 * @param details
	 *            消息详情集合
	 * @param msgId
	 *            消息Id
	 */
	public void doSubmit(List<MsgDetailEntity> details, long msgId) {
		ses.execute(new Runnable() {
			@Override
			public void run() {
				List<MsgRecordEntity> records = new ArrayList<>();
				List<MsgRecordEntity> msgRds = null;
				List<UserEntity> users = null;
				MsgRecordEntity record = null;
				for (MsgDetailEntity msgDetailEntity : details) {
					// 用户
					if (RevieveType.TYPE_1.getValue() == msgDetailEntity.getRevieveType()) {
						record = new MsgRecordEntity();
						record.setMsgId(msgId);
						record.setMsgDetailId(msgDetailEntity.getId());
						record.setRecieveId(msgDetailEntity.getReviever());
						record.setMsgStatus(Constants.NUMBER_ONE);
						record.setCreateTime(new Timestamp(DateUtils.getCurrentDateTime().getTime()));
						record.setUpdateTime(new Timestamp(DateUtils.getCurrentDateTime().getTime()));
						record.setDelFlag(Constants.NUMBER_ONE);
						records.add(record);
					}
					// 区域
					else if (RevieveType.TYPE_2.getValue() == msgDetailEntity.getRevieveType()) {
						users = userService.getUsersByAreaId(String.valueOf(msgDetailEntity.getReviever()));
					}
					// 部门
					else if (RevieveType.TYPE_3.getValue() == msgDetailEntity.getRevieveType()) {
						users = userService.getUsersByOfficeId(String.valueOf(msgDetailEntity.getReviever()));
					}
					// 全部
					else if (RevieveType.TYPE_4.getValue() == msgDetailEntity.getRevieveType()) {
						users = userService.getAllUsers();
					}

					// 组装消息记录实体
					msgRds = populateMsgRecord(users, msgId, msgDetailEntity.getId());
					if (!CollectionUtils.isEmpty(msgRds)) {
						records.addAll(msgRds);
					}
					if (!CollectionUtils.isEmpty(records)) {
						Pagination page = new Pagination(Constants.DEFAULT_PAGE_SIZE, records.size());
						for (int i = 0; i < page.getPageCount(); i++) {
							if (page.getToIndex() > records.size()) {
								msgRds = records.subList(page.getFromIndex(), records.size());
							} else {
								msgRds = records.subList(page.getFromIndex(), page.getToIndex());
							}
							// 批量添加消息记录数据
							sysMsgService.batchAddMsgRecordInfo(msgRds);
						}
					}

					records.clear();
				}
				sysMsgService.deleMsgRecordInfo(msgId);
			}
		});
	}

	/**
	 * 组装消息记录实体
	 * 
	 * @param users
	 *            用户集合
	 * @param msgId
	 *            消息Id
	 * @param detailId
	 *            消息明细Id
	 */
	private List<MsgRecordEntity> populateMsgRecord(List<UserEntity> users, long msgId, long detailId) {
		List<MsgRecordEntity> records = new ArrayList<>();
		MsgRecordEntity record = null;
		if (!CollectionUtils.isEmpty(users)) {
			for (UserEntity user : users) {
				if (null != user) {
					record = new MsgRecordEntity();
					record.setMsgId(msgId);
					record.setMsgDetailId(detailId);
					record.setRecieveId(user.getId());
					record.setMsgStatus(Constants.NUMBER_ONE);
					record.setCreateTime(new Timestamp(DateUtils.getCurrentDateTime().getTime()));
					record.setUpdateTime(new Timestamp(DateUtils.getCurrentDateTime().getTime()));
					record.setDelFlag(Constants.NUMBER_ONE);
					records.add(record);
				}
			}
		}
		return records;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		// 初始化任务线程池
		ses = Executors.newSingleThreadExecutor();
	}
}